Attribute VB_Name = "modFunctions"
Option Explicit
Public adoConn As ADODB.Connection
Public strCurrentLogFileName As String

Public Type TrackingVariables
    ComId As String
    BuId As String
    ReferenceId As String
    ReqSeqNum As String
    RequestStatus As String
    MsgId As String
End Type
'Public Tracking As TrackingVariables

Public Type RequestorVariables
    Date As String
    Time As String
    Name As String
    UserId As String
    Email As String
End Type
'Public Request As RequestorVariables

Public Type IndexVariables
    InterfaceNo As Long
    XmlString As String
    CreatedDated As String
    SarsResponseYn As String
    KeyTaxType As String
    MqStatus As String
    Tracking As TrackingVariables
    Request As RequestorVariables
End Type
Public IdxVars As IndexVariables

'Public Type FileFormatErrorsVars
'    Code As String
'    Reason As String
'    Text As String
'    Line As String
'End Type
'Public FormatErr As FileFormatErrorsVars
'
'Public Type DataValidErrorsVars
'    DataErrText  As String
'End Type
'Public DataErr As DataValidErrorsVars
'
'Public Type ErrorVAriables
'    InterfaceNo As Long
'    ErrorType As String
'    FormatErr As FileFormatErrorsVars
'    DataErr As DataValidErrorsVars
'End Type
'Public ErrVars As ErrorVAriables


Public objFileError As clsFileApprove
Public objFunctions As clsApproveFunctions
Public objReg As clsReg
Public objSendEMail As clsSendEMail

'Registry Function Prototypes
Declare Function RegOpenKeyEx Lib "advapi32" _
  Alias "RegOpenKeyExA" ( _
  ByVal hKey As Long, _
  ByVal lpSubKey As String, _
  ByVal ulOptions As Long, _
  ByVal samDesired As Long, _
  phkResult As Long _
) As Long

Declare Function RegQueryValueEx Lib "advapi32" _
  Alias "RegQueryValueExA" ( _
  ByVal hKey As Long, _
  ByVal lpValueName As String, _
  ByVal lpReserved As Long, _
  ByRef lpType As Long, _
  ByVal szData As String, _
  ByRef lpcbData As Long _
) As Long

Declare Function RegCloseKey Lib "advapi32" ( _
  ByVal hKey As Long _
) As Long

Type TSECURITY_ATTRIBUTES
  nLength As Long
  lpSecurityDescriptor As Long
  bInheritHandle As Boolean
End Type

Declare Function RegCreateKeyEx Lib "advapi32" _
  Alias "RegCreateKeyExA" ( _
  ByVal hKey As Long, _
  ByVal lpSubKey As String, _
  ByVal Reserved As Long, _
  ByVal lpClass As String, _
  ByVal dwOptions As Long, _
  ByVal samDesired As Long, _
  lpSecurityAttributes As TSECURITY_ATTRIBUTES, _
  phkResult As Long, _
  lpdwDisposition As Long _
) As Long

Declare Function RegSetValueEx Lib "advapi32" _
  Alias "RegSetValueExA" ( _
  ByVal hKey As Long, _
  ByVal lpValueName As String, _
  ByVal Reserved As Long, _
  ByVal dwType As Long, _
  ByVal szData As String, _
  ByVal cbData As Long _
) As Long


'********************************************************************************************************
'##ModelId=3521FD15008C
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

'##ModelId=3521FD1602DA
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long


Public Function ConnectToDb(strErrMsg As String) As Boolean
On Error GoTo ConnectToDbFix
    
    Set adoConn = New ADODB.Connection
    With adoConn
        .CursorLocation = adUseClient
        .ConnectionString = GetConnectionString() 'Johann (20090625): Centralised code for connection string added
        .open
    End With
    
    ConnectToDb = True

    Exit Function
    
ConnectToDbFix:
    ConnectToDb = False
    strErrMsg = Err.Description
End Function

Public Sub WriteToErr(strMsg As String)

    Dim strFileName As String
    Dim intFreeFile As Integer
    Dim strHeaderString As String
    Dim lngIdxFileCount As Long
    
    On Error GoTo WriteToLogFix
        
    'MsgBox "Error: " & strMsg
        
    
    GetNextLogFileName
    
    strHeaderString = "Error occurred in FileApprove: "
    'text = 23
    'max lngIdxFileCount = 10 digits
    'max strheader = 35
    
    'left just
    strHeaderString = Format(strHeaderString, "!" & String(35, "@"))
    'right just
    'strHeaderString = Format(strHeaderString, String(35, "@"))
    
    
    strFileName = objReg.LogFilePath & strCurrentLogFileName
    'change from log to err
    strFileName = Left(strFileName, Len(strFileName) - 3)
    strFileName = strFileName & "err"
              
    strMsg = strMsg & " (" & Now & ")"

     'open/write to file
    intFreeFile = FreeFile
    Open strFileName For Binary Access Write As #intFreeFile
        Put #intFreeFile, , strHeaderString
    Close #intFreeFile
    
    Open strFileName For Append As #intFreeFile
        If lngIdxFileCount = 0 Then
            Print #intFreeFile,  'get to next to next line after header
        End If
        Print #intFreeFile, strMsg
    Close #intFreeFile

    objSendEMail.SendErrorEmail strMsg

    objReg.LogErrors = 1
    
    
    Exit Sub
    
WriteToLogFix:
    MsgBox Err.Number & Err.Description
End Sub
Public Sub GetNextLogFileName()
    
    Dim lngSeqNo As Long
    Dim strEnvr As String
    
    lngSeqNo = objReg.LogFileSeq
    lngSeqNo = lngSeqNo + 1
    
    strCurrentLogFileName = "KT" & strEnvr & LeftPad(CStr(lngSeqNo), "0", 5) & ".log"

    objReg.LogFileSeq = lngSeqNo

End Sub
Public Function LeftPad(PadStr As String, PadChar As String, PadLen As Integer) As String

    While Len(PadStr) < PadLen
        PadStr = PadChar & PadStr
    Wend
    LeftPad = PadStr
    
End Function
